﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NObsidian.Containers;
using NObsidian.Envelopes;

namespace NObsidian.Managers
{
    public class CharacterManager : ManagerBase
    {
        public CharacterManager(ObsidianProxy proxy)
            : base(proxy)
        {
        }

        public List<Character> GetCampaignCharacterIndex(string campaignId)
        {
            string charindex;
            string url;

            url = ComposeCharacterIndexURL(campaignId);
            charindex = _proxy.GetObsidianData(url);

            return _proxy.GetObjectFromJson<List<Character>>(charindex);
        }

        public Character GetCharacter(string campaignId, string characterId)
        {
            string character;
            string url;

            url = ComposeCharacterURL(campaignId, characterId);
            character = _proxy.GetObsidianData(url);

            return _proxy.GetObjectFromJson<Character>(character);
        }

        public Character CreateCharacter(string campaignId, Character character)
        {
            Character c;
            CharacterEnvelope envelope;
            string url;
            string response;

            url = ComposeCharacterIndexURL(campaignId);
            c = character.GetMinimumUpdateObject<Character>();
            envelope = new CharacterEnvelope { Character = c };

            response = _proxy.CreateObsidianData(url, _proxy.GetJSONFromObject<CharacterEnvelope>(envelope));
            c = _proxy.GetObjectFromJson<Character>(response);

            return c;
        }

        public Character UpdateCharacter(string campaignId, string characterId, Character character)
        {
            Character c;
            CharacterEnvelope envelope;
            string url;
            string response;

            url = ComposeCharacterURL(campaignId, characterId);
            c = character.GetMinimumUpdateObject<Character>();
            envelope = new CharacterEnvelope { Character = c };

            response = _proxy.UpdateObsidianData(url, _proxy.GetJSONFromObject<CharacterEnvelope>(envelope));
            c = _proxy.GetObjectFromJson<Character>(response);

            return c;
        }


        public void DeleteCharacter(string campaignId, string characterId)
        {
            string url;

            url = ComposeCharacterURL(campaignId, characterId);
            _proxy.DeleteObsidianData(url);
        }

        private string ComposeCharacterIndexURL(string id)
        {
            StringBuilder sb = new StringBuilder();

            return sb.Append(CAMPAIGNS_URL).Append(id).Append("/characters.json").ToString();
        }

        private string ComposeCharacterURL(string campaignId, string charId)
        {
            StringBuilder sb = new StringBuilder();

            return sb.Append(CAMPAIGNS_URL).Append(campaignId).Append("/characters/").Append(charId).Append(".json").ToString();
        }



    }
}
